#include "RobotMap.h"
#include "ui_RobotMap.h"

RobotMap::RobotMap(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RobotMap)
{
    ui->setupUi(this);
    initMapSystem();

//    RobotItem* item1 = new RobotItem(0, m_mapScene->sceneRect(), 0, 0);
//    addRobotItem(item1);
//    item1->setPos(-100, -100);

//    connect(ui->mapView, &MapView::mouseClicked, this, [&](const QPoint& point){
//        QPointF scenePoint = ui->mapView->mapToScene(point);
//        ui->scenePoint->setText(QString::asprintf("sceneSize:%.0f, %.0f,",
//                                                  scenePoint.x(),
//                                                  scenePoint.y()));
//        QPointF oldPoint = m_robotItemVector[0]->pos();
//        qreal angel = -std::atan( (scenePoint.y() - oldPoint.y()) / (scenePoint.x() - oldPoint.y()) );
//        m_robotItemVector[0]->setRotation( (angel / 3.14)*180 );
//        m_robotItemVector[0]->setPos(scenePoint);
//    });

}

RobotMap::~RobotMap()
{
    delete ui;
}

void RobotMap::addRobotItem(RobotItem *item)
{
    m_robotItemVector.push_back(item);
    m_mapScene->addItem(item);
}

void RobotMap::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    //1.获取场景大小
    QRectF sceneRect = m_mapScene->sceneRect();
    //2.显示
    ui->viewSize->setText(QString::asprintf("视角大小:%d, %d",
                                            ui->mapView->width(),
                                            ui->mapView->height()));
    ui->sceneSize->setText(QString::asprintf("地图大小:%.0f, %.0f, %.0f, %.0f",
                                            sceneRect.left(),
                                            sceneRect.top(),
                                            sceneRect.width(),
                                            sceneRect.height()));
}

void RobotMap::initMapSystem()
{
    QRect rect(-250, -250, 500, 500);
    m_mapScene = new MapScene(rect);
    ui->mapView->setScene(m_mapScene);
    ui->mapView->setRenderHint(QPainter::Antialiasing, true);

    m_borderItem = new QGraphicsRectItem(rect);
//    m_borderItem->setFlags( QGraphicsItem::ItemIsSelectable |
//                    QGraphicsItem::ItemIsFocusable);
    QPen pen;
    pen.setWidth(3);
    m_borderItem->setPen(pen);
    m_mapScene->addItem(m_borderItem);
}

